package com.share.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Method;

/**
 * author:caifan
 * date:2020/2/7
 */
@Intercepts({@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )}
)
public class ExecutorInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        System.out.println("Executor:");
        Object[] args = invocation.getArgs();
        for (Object obj : args) {
            if (null != obj) {
                System.out.println(obj.toString());
            }
        }
        Method method = invocation.getMethod();
        System.out.println("method:" + method.getName());
        Object target = invocation.getTarget();
        System.out.println("target:" + target.toString());
        Object proceed = invocation.proceed();
        System.out.println("proceed:" + proceed.toString());
        System.out.println("===============");
        return proceed;
    }
}
